package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bomp.facade.base.Header;
import cc.rengu.igas.bomp.facade.request.UserInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.UserInfoQueryResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.UserInfoBean;
import cc.rengu.igas.bsps.facade.request.BompUserInfoQueryRequest;
import cc.rengu.igas.bsps.facade.response.BompUserInfoQueryResponse;
import cc.rengu.igas.share.common.constant.ShareParamConstant;
import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class BompUserInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        BompUserInfoQueryRequest request = new BompUserInfoQueryRequest();
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BompUserInfoQueryRequest bompUserInfoQueryRequest = (BompUserInfoQueryRequest) request;
        BizResponse bizResponse = new BizResponse();
        BompUserInfoQueryResponse response = new BompUserInfoQueryResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        String instId = bompUserInfoQueryRequest.getHeader().getInstId();
        if (StringUtil.isEmptyOrNull(instId)) {
            rglog.error("必传参数instId法人号为空！", instId);
            bizResponse.setRespCode(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            bizResponse.setResult(response);
            return bizResponse;
        }
        response.setHeader(bompUserInfoQueryRequest.getHeader());
        List<UserInfoBean> userInfoBeans = getUserInfo(bompUserInfoQueryRequest);
        if (CollectionUtils.isEmpty(userInfoBeans)) {
            bizResponse.setResult(response);
            bizResponse.setRespCode(RespCodeEnum.USER_INFO_QUERY_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.USER_INFO_QUERY_ERROR.getRespDesc());
            return bizResponse;
        }
        response.setUserInfoList(userInfoBeans);
        bizResponse.setResult(response);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        BompUserInfoQueryResponse bompUserInfoQueryResponse = (BompUserInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(bompUserInfoQueryResponse);
    }

    public List<UserInfoBean> getUserInfo(BompUserInfoQueryRequest bompUserInfoQueryRequest) throws Exception {
        String callMode = "1";
        callMode = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.APPLICATION_CONF_NODE, AppConfigConstant.BOMP_SERVICE_CALL_MODE);
        if (!StringUtil.isEmptyOrNull(callMode)) {
            if (!"0".equals(callMode.trim()) && !"1".equals(callMode.trim())) {
                rglog.error("内管相关信息获取调用模式<{}>参数配置错误:0-本地调用，1-微服务调用!", callMode.trim());
                throw new BizException("内管相关信息获取方式配置有误:<{}>", callMode);
            } else {
                callMode = callMode.trim();
            }
        }
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        rglog.info("正在获取用户信息", "0".equals(callMode) ? "本地调用" : "微服务调用");
        Header header = new Header();
        header.setInstId(instId);
        header.setSrcSysId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.HEADER_SRC_SYS_ID));
        header.setChanlId(ChannelEnum.UPMP.getChannelType());
        header.setTxnNum("M00BM057");
        header.setBizType("00");
//        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setVersion(ShareParamConstant.DUBBO_VSERSION);
        header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
        header.setTransTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_TIME));
        header.setTransDate(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_DATE));
        UserInfoQueryRequest userInfoQueryRequest = new UserInfoQueryRequest();
        userInfoQueryRequest.setHeader(header);
        userInfoQueryRequest.setQueryType(bompUserInfoQueryRequest.getQueryType());
        userInfoQueryRequest.setUserId(bompUserInfoQueryRequest.getUserId());
        userInfoQueryRequest.setUserInst(bompUserInfoQueryRequest.getUserInst());
        userInfoQueryRequest.setCertNo(bompUserInfoQueryRequest.getCertNo());
        userInfoQueryRequest.setCertType(bompUserInfoQueryRequest.getCertType());
        userInfoQueryRequest.setLoginId(bompUserInfoQueryRequest.getLoginId());
        userInfoQueryRequest.setOpenId(bompUserInfoQueryRequest.getOpenId());
        userInfoQueryRequest.setPhoneNo(bompUserInfoQueryRequest.getPhoneNo());
        Result<UserInfoQueryResponse> callResult = null;
        DubboService dubboService = new DubboServiceImpl();
        if (!"0".equals(callMode)) {
            callResult = (Result<UserInfoQueryResponse>) dubboService.callDubboService(ShareServiceConstant.BOMP_USER_MANAGE_SERVICE, ShareServiceConstant.BOMP_USER_INFO_QUERY_METHOD, userInfoQueryRequest);
        }
        if (null == callResult || null == callResult.getResult() ||  CollectionUtils.isEmpty(callResult.getResult().getUserInfoList())) {
            rglog.info("查询参数信息失败");
            return null;
        }
        List<cc.rengu.igas.bomp.facade.bean.UserInfoBean> userInfoList = callResult.getResult().getUserInfoList();
        List<UserInfoBean> userInfoBeans = new ArrayList<>();

        if (!CollectionUtils.isEmpty(userInfoList)) {
            for (cc.rengu.igas.bomp.facade.bean.UserInfoBean userInfoBean : userInfoList) {
                UserInfoBean userInfoBean1 = new UserInfoBean();
                BeanUtil.beanCopy(userInfoBean, userInfoBean1);
                userInfoBeans.add(userInfoBean1);
            }
        }
        return userInfoBeans;
    }
}
